
package com.fx.apartment.modules.activity.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fx.apartment.common.tool.lock.RedisLock;
import com.fx.apartment.modules.activity.entity.Activity;
import com.fx.apartment.modules.activity.service.IActivityParticipationService;
import com.fx.apartment.modules.activity.service.IActivityService;
import com.fx.apartment.common.support.Condition;
import com.fx.apartment.common.support.Query;
import com.fx.apartment.common.tool.api.R;
import com.fx.apartment.common.tool.utils.Func;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import lombok.AllArgsConstructor;
import javax.validation.Valid;

import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fx.apartment.modules.activity.entity.ActivityVotes;
import com.fx.apartment.modules.activity.service.IActivityVotesService;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

/**
 * 投票纪录 控制器
 *
 * @author QinXueFeng
 * @since 2021-04-19
 */
@RestController
@AllArgsConstructor
@RequestMapping("/activityvotes")
@Api(value = "投票纪录", tags = "投票纪录接口")
public class ActivityVotesController {

	private final IActivityVotesService activityVotesService;

	private final IActivityParticipationService activityParticipationService;

	private final IActivityService activityService;
	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入activityVotes")
	public R<ActivityVotes> detail(ActivityVotes activityVotes) {
		ActivityVotes detail = activityVotesService.getOne(Condition.getQueryWrapper(activityVotes));
		return R.data(detail);
	}

	/**
	 * 分页 投票纪录
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入activityVotes")
	public R<IPage<ActivityVotes>> list(ActivityVotes activityVotes, Query query) {
		IPage<ActivityVotes> pages = activityVotesService.page(Condition.getPage(query), Condition.getQueryWrapper(activityVotes));
		return R.data(pages);
	}


	/**
	 * 新增 投票纪录
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入activityVotes")
	@RedisLock("activityvotes::save")
	public R save(@Valid @RequestBody ActivityVotes activityVotes) {
		Activity activity = activityService.getById(activityVotes.getActivityId());
		String startTime = LocalDateTime.of(LocalDate.now(), LocalTime.MIN).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
		String endTime = LocalDateTime.of(LocalDate.now(), LocalTime.MAX).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
		int count = activityVotesService.count(Wrappers.lambdaQuery(activityVotes).between(ActivityVotes::getCreateTime, startTime,endTime));
		if(count>=activity.getVotesCount()){
			return R.fail("");
		}
		return R.status(activityVotesService.save(activityVotes)&&activityParticipationService.addVotesNum(activityVotes.getParticipationId()));
	}

	
	/**
	 * 删除 投票纪录
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 8)
	@ApiOperation(value = "删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(activityVotesService.removeByIds(Func.toLongList(ids)));
	}

	
}
